package fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.RecommendDbEntity;

import java.util.ArrayList;
import java.util.List;

import dbdao.RecommendDBHelper;
import entity.FocusEntity;
import fragment.tabhostFragment.PersonalDetailsFragment;
import tools.ItemOnClickListenerOther;
import utils.viewtools.SpacesItemDecoration;
import utils.adapter.FocusonAdpter;
import volley.VolleySingleton;


/**
 * 关注 fragment
 * Created by dllo on 15/11/17.
 */
public class FocusonPageFragment extends AbsBaseFragment implements ItemOnClickListenerOther {
    private RecyclerView recyclerView;
    private FocusonAdpter adpter;
    private String Url = "http://mobile.ximalaya.com/mobile/api/1/feed/unlogin/dynamic?device=android";

    private RecommendDBHelper helper;
    @Override
    protected int getLayoutView() {
        return R.layout.fragment_page_focuson;
    }

    @Override
    protected void initView() {
        recyclerView = findView(R.id.focusonRecylerView);
        helper = RecommendDBHelper.getInstance();
    }

    @Override
    protected void initData() {
        adpter = new FocusonAdpter(getContext());
        adpter.setItemOnClick(this);
        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adpter);
        recyclerView.addItemDecoration(new SpacesItemDecoration(10));
        initNetwork();
    }

    private void initNetwork() {
        VolleySingleton.getInstance().addRequest(Url,
                FocusEntity.class, new Response.Listener<FocusEntity>() {
                    @Override
                    public void onResponse(FocusEntity response) {
                        if (response != null) {
                            List<FocusEntity.DataEntity> datas = response.getData();
                            if (datas != null) {
                                adpter.addDatas(datas);
                            }
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }

    @Override
    public void itemListener(Object o, int i) {
        FocusEntity.DataEntity f = (FocusEntity.DataEntity) o;
        switch (i) {
            case 1:

                Toast.makeText(getContext(), "关注", Toast.LENGTH_SHORT).show();
                //点击关注的时候存数据
                List<RecommendDbEntity>datas = new ArrayList<>();
                RecommendDbEntity entity = new RecommendDbEntity();
                entity.setNickname(f.getNickname());
                entity.setSmallLogo(f.getAvatar());
                entity.setPlayUrl(f.getPlayPath32());
                entity.setUid(f.getTrackId());
                entity.setAlbmid(f.getAlbumId());
                entity.setTitle(f.getAlbumTitle());
                datas.add(entity);
                helper.saveRecommend(datas);

                break;
            case 2:
                goToPlay(f.getTrackId());

                break;
            case 3:
                goToNext(new PersonalDetailsFragment(f.getUid()));
                break;
        }
    }
}
